package com.louzhu.gulimall.product.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池配置
 */
@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
        return new ThreadPoolExecutor(
                // 核心线程池大数
                pool.getCoreSize(),
                // 最大线程数
                pool.getMaxSize(),
                // 非核心线程最大空闲时间
                pool.getKeepAliveTime(),
                // 时间单位
                TimeUnit.SECONDS,
                // 阻塞队列最大容量
                new LinkedBlockingDeque<>(100000),
                // 默认线程工厂
                Executors.defaultThreadFactory(),
                // 阻塞策略-抛弃
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
